#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int hash[100001] = { 0 };
        int left = 0, right = 0, n = fruits.size(), ret = 0, kind = 0;
        while (right < n) {
            if (hash[fruits[right]] == 0) {
                kind++;
            }
            hash[fruits[right]]++;

            while (kind > 2) {
                hash[fruits[left]]--;
                if (hash[fruits[left]] == 0)
                    kind--;
                left++;

            }
            ret = max(ret, right - left + 1);
            right++;

        }
        return ret;
    }
};